package com.example.day20_zhongji.fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day20_zhongji.R;
import com.example.day20_zhongji.adapter.HomeAdapter;
import com.example.day20_zhongji.bean.ArticleBean;
import com.example.day20_zhongji.bean.BannerBean;
import com.example.day20_zhongji.presenter.ArticlePresenter1;
import com.example.day20_zhongji.view.ArticleView1;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements ArticleView1 {


    private RecyclerView mHomeRecycler;
    private ArrayList<ArticleBean.DataBean.DatasBean> datasBeans;
    private ArrayList<BannerBean.DataBean> dataBeans;
    private HomeAdapter homeAdapter;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        ArticlePresenter1 articlePresenter1 = new ArticlePresenter1(this);
        articlePresenter1.getdata();
        mHomeRecycler = view.findViewById(R.id.home_recycler);
        mHomeRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mHomeRecycler.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
        datasBeans = new ArrayList<>();
        dataBeans = new ArrayList<>();
        homeAdapter = new HomeAdapter(getContext(), datasBeans, dataBeans);
        mHomeRecycler.setAdapter(homeAdapter);
    }

    @Override
    public void onSuccess(ArticleBean data) {
        datasBeans.addAll(data.getData().getDatas());
        Log.i("aaa", "onSuccess: "+data.getData().getDatas());
        homeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFali(String error) {
        Toast.makeText(getContext(),error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSuccess(BannerBean data) {
        dataBeans.addAll(data.getData());
        Log.i("aaa", "onSuccess: "+data.getData());
        homeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFali1(String error) {
        Toast.makeText(getContext(),error,Toast.LENGTH_LONG).show();

    }
}
